﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Reflection;
using System.Linq.Expressions;

namespace PDXTests
{
    public static class Utils
    {
        public static void Load(this UIElement element)
        {
            var window = new System.Windows.Window();
            window.Content = element;
            window.Show();
            window.Close();
        }

        public static object AccessProperty(this object obj, string propertyName)
        {
            var property = obj.GetType().GetProperty(propertyName,
                System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            return property.GetValue(obj, null);
        }

        public static PropertyInfo PropertyFor<T, V>(this T obj, Expression<Func<T, V>> expression)
        {
            var body = expression.Body as MemberExpression;
            if (body == null)
            {
                throw new Exception("expression.Body must be a Property");
            }

            var property = body.Member as PropertyInfo;
            if (property == null)
            {
                throw new Exception("expression.Body must be a Property");
            }

            return property;
        }
    }
}
